import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import { formatMessage, FormattedMessage } from 'umi/locale';
import { Button, Table, Divider, Popconfirm, Row } from 'antd';
import AddPropertyForm from './add';
import EditPropertyForm from './edit';
import ViewPropertyForm from './view';

@connect(({ productmodel, loading }) => ({
  productmodel,
  loading: loading.models.productmodel,
}))
class ProductModelProperty extends PureComponent {
  state = {
    addModalVisible: false,
    editModalVisible: false,
    viewModalVisible: false,
  };

  componentDidMount() {
    const { dispatch, productModelId } = this.props;
    dispatch({
      type: 'productmodel/fetchProperty',
      payload: { modelId: productModelId },
    });
    dispatch({
      type: 'productmodel/fetchUnit',
    });
    dispatch({
      type: 'productmodel/fetchDataType',
    });
  }

  handleDelete = id => {
    const { dispatch, productModelId } = this.props;
    dispatch({
      type: 'productmodel/delProperty',
      payload: id,
      productModelId,
    });
  };

  handleView = id => {
    const { dispatch } = this.props;
    const { handleViewModalVisible } = this;
    dispatch({
      type: 'productmodel/showView',
      payload: id,
      handleViewModalVisible,
    });
  };

  editView = id => {
    const { dispatch } = this.props;
    const { handleEditModalVisible } = this;
    dispatch({
      type: 'productmodel/fetchPropertyDetail',
      payload: id,
      handleEditModalVisible,
    });
  };

  handleAddModalVisible = flag => {
    this.setState({
      addModalVisible: !!flag,
    });
  };

  handleEditModalVisible = flag => {
    this.setState({
      editModalVisible: !!flag,
    });
  };

  handleViewModalVisible = flag => {
    this.setState({
      viewModalVisible: !!flag,
    });
  };

  handleAdd = (fields, form) => {
    const { dispatch, productModelId } = this.props;
    const { handleAddModalVisible } = this;
    dispatch({
      type: 'productmodel/addProperty',
      payload: {
        modelId: productModelId,
        name: fields.name,
        label: fields.label,
        dataTypeId: fields.dataTypeId,
        unitId: fields.unitId,
        range: fields.range,
        desc: fields.desc,
      },
      form,
      handleAddModalVisible,
    });
  };

  handleEdit = (fields, form) => {
    const { dispatch } = this.props;
    const { handleEditModalVisible } = this;
    dispatch({
      type: 'productmodel/editProperty',
      payload: {
        id: fields.id,
        modelId: fields.modelId,
        name: fields.name,
        label: fields.label,
        dataTypeId: fields.dataTypeId,
        unitId: fields.unitId,
        range: fields.range,
        desc: fields.desc,
      },
      form,
      handleEditModalVisible,
    });
  };

  columns = [
    {
      key: 'name',
      title: formatMessage({ id: 'app.mm.productmodel.property.table.name' }),
      dataIndex: 'name',
    },
    {
      key: 'label',
      title: formatMessage({ id: 'app.mm.productmodel.property.table.label' }),
      dataIndex: 'label',
    },
    {
      key: 'dataTypeName',
      title: formatMessage({ id: 'app.mm.productmodel.property.table.dataType' }),
      dataIndex: 'dataTypeName',
    },
    {
      key: 'range',
      title: formatMessage({ id: 'app.mm.productmodel.property.table.range' }),
      dataIndex: 'range',
    },
    {
      key: 'unitName',
      title: formatMessage({ id: 'app.mm.productmodel.property.table.unit' }),
      dataIndex: 'unitName',
    },
    {
      key: 'desc',
      title: formatMessage({ id: 'app.mm.productmodel.property.table.desc' }),
      dataIndex: 'desc',
    },
    {
      key: 'createTime',
      title: formatMessage({ id: 'app.mm.productmodel.property.table.createTime' }),
      dataIndex: 'createTime',
    },
    {
      key: 'action',
      title: formatMessage({ id: 'app.mm.productmodel.property.table.action' }),
      render: (text, record) => (
        <Fragment>
          <a onClick={() => this.handleView(record.id)}>
            <FormattedMessage id="app.mm.productmodel.property.table.action.view" />
          </a>
          <Divider type="vertical" />
          <a onClick={() => this.editView(record.id)}>
            <FormattedMessage id="app.mm.productmodel.property.table.action.edit" />
          </a>
          <Divider type="vertical" />
          <Popconfirm
            title={
              <div style={{ width: 280 }}>
                <div style={{ fontWeight: 'bold', color: '000', marginBottom: 10 }}>
                  <span>
                    {formatMessage(
                      { id: 'app.mm.productmodel.property.del.confirm' },
                      { name: record.name },
                    )}
                  </span>
                </div>
              </div>
            }
            onConfirm={() => this.handleDelete(record.id)}
          >
            <a>
              <FormattedMessage id="app.mm.productmodel.property.table.action.del" />
            </a>
          </Popconfirm>
        </Fragment>
      ),
    },
  ];

  render() {
    const {
      productmodel: { propertyList, pagination, unitList, dataTypeList, current, propertyDetail },
      loading,
      dispatch,
      productModelId,
    } = this.props;

    const paginationProps = {
      showSizeChanger: true,
      showQuickJumper: true,
      ...pagination,
      onShowSizeChange(curr, size) {
        dispatch({
          type: 'productmodel/fetchProperty',
          payload: { modelId: productModelId, ...{ pageNum: curr, pageSize: size } },
        });
      },
      onChange(curr) {
        dispatch({
          type: 'productmodel/fetchProperty',
          payload: {
            modelId: productModelId,
            ...{ pageNum: curr, pageSize: pagination.currentSize },
          },
        });
      },
    };

    const addModalProps = {
      current,
      handleModalVisible: this.handleAddModalVisible,
      handleAdd: this.handleAdd,
      units: unitList,
      dataTypes: dataTypeList,
    };
    const editModalProps = {
      current,
      handleModalVisible: this.handleEditModalVisible,
      handleEdit: this.handleEdit,
      units: unitList,
      dataTypes: dataTypeList,
      propertyInfo: propertyDetail,
    };
    const viewModalProps = {
      current,
      handleModalVisible: this.handleViewModalVisible,
      propertyInfo: propertyDetail,
    };
    const { addModalVisible, editModalVisible, viewModalVisible } = this.state;
    return (
      <Fragment>
        <Row style={{ textAlign: 'right' }}>
          <Button type="primary" onClick={() => this.handleAddModalVisible(true)}>
            <FormattedMessage id="app.mm.productmodel.property.add" />
          </Button>
        </Row>
        <Row>
          <Table
            columns={this.columns}
            dataSource={propertyList}
            pagination={paginationProps}
            loading={loading}
            rowKey="id"
            style={{ marginTop: 15 }}
          />
        </Row>
        <AddPropertyForm {...addModalProps} modalVisible={addModalVisible} />
        <EditPropertyForm {...editModalProps} modalVisible={editModalVisible} />
        <ViewPropertyForm {...viewModalProps} modalVisible={viewModalVisible} />
      </Fragment>
    );
  }
}
export default ProductModelProperty;
